const express = require('express')
const app = express()
const path = require('path')
const port = 3000
// 跨域
const cors = require('cors')
// 为所有源开启跨域
// 1
app.use(cors())
//  2
/* app.all('*', function (req, res, next) {
  // 允许跨域的域名，*代表允许任意域名跨域
  res.header('Access-Control-Allow-Origin', '*')
  // 允许跨域的请求头
  res.header('Access-Control-Allow-Headers', 'content-type')
  // 允许跨域的请求方法
  res.header('Access-Control-Allow-Methods', 'DELETE,PUT,POST,GET,OPTIONS')
  next()
}) */

// 解析body
app.use(express.urlencoded({ extended: false }))
// 解析json
app.use(express.json())
// 开放静态资源
app.use('/static', express.static(path.join(__dirname, 'public')))
app.post('/login', (req, res) => {
  console.log(req)
  res.send('Hello World!')
  console.log(req)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
